package nku.csc364.updatedatabase;

import nku.csc364.R;
import nku.csc364.databasehelper.DataHelper;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class UpdateDatabaseItemActivity extends Activity{
	Spinner itemProductSpinner, itemItemSpinner, itemSupplierSpinner;
	EditText itemName, itemStatus, itemListPrice, itemUnitCost;
	Integer imageInteger;
	ImageView itemImageView;
	DataHelper dh = null; 
	
	private Integer[] Imgid = {
			R.drawable.proj3_1, 
			R.drawable.proj3_2, 
			R.drawable.proj3_3, 
			R.drawable.proj3_4, 
			R.drawable.proj3_5, 
			R.drawable.proj3_6, 
			R.drawable.proj3_7,
			R.drawable.proj3_8,
			R.drawable.proj3_9,
			R.drawable.proj3_10,
			R.drawable.proj3_11,
			R.drawable.proj3_12,
			R.drawable.proj3_13,
			R.drawable.proj3_14 
			};
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item);
        
        itemProductSpinner = (Spinner)findViewById(R.id.itemSpinner1);
        itemItemSpinner = (Spinner)findViewById(R.id.itemSpinner2);
        itemSupplierSpinner = (Spinner)findViewById(R.id.itemSpinner3);
        itemName = (EditText)findViewById(R.id.itemEditText1);
        itemStatus = (EditText)findViewById(R.id.itemEditText2);
        itemListPrice = (EditText)findViewById(R.id.itemEditText3);
        itemUnitCost = (EditText)findViewById(R.id.itemEditText4);
        itemImageView = (ImageView)findViewById(R.id.itemImageView1);
        
        dh = DataHelper.getDataHelper(this);
        setProductSpinner();
        itemProductSpinner.setPrompt("Select...");
        setItemSpinner();
        itemItemSpinner.setPrompt("Select...");
        setSupplierSpinner();
        itemSupplierSpinner.setPrompt("Select...");
        itemItemSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
    }
    
	  public class MyOnItemSelectedListener implements OnItemSelectedListener {
		    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
		    	if(!parent.getItemAtPosition(pos).toString().equals(" ")){
		    		Cursor mCursor = dh.getItem(parent.getItemAtPosition(pos).toString());
			    	int i = 0;
					for(String x : mCursor.getColumnNames()){
						if(i == 0){
							String product = mCursor.getString(mCursor.getColumnIndexOrThrow(x));
							ArrayAdapter<CharSequence> productArray = getProductAdapter();
							int index = 0;
							for(int j = 0; j < productArray.getCount(); j++){
								if(product.equals(productArray.getItem(j))){
									index = j;
								}
							}
							itemProductSpinner.setSelection(index);
						}
						else if( i == 1){
							itemName.setText(mCursor.getString(mCursor.getColumnIndexOrThrow(x)));	
						}
						else if ( i == 2){
							itemStatus.setText(mCursor.getString(mCursor.getColumnIndexOrThrow(x)));
						}
						else if ( i == 3){
							Float listPrice = mCursor.getFloat(mCursor.getColumnIndexOrThrow(x));
							itemListPrice.setText(listPrice.toString());
						}
						else if ( i == 4){
							Float unitCost = mCursor.getFloat(mCursor.getColumnIndexOrThrow(x));
							itemUnitCost.setText(unitCost.toString());
						}
						else if ( i == 5){
							String supplier = mCursor.getString(mCursor.getColumnIndexOrThrow(x));
							ArrayAdapter<CharSequence> supplierArray = getSupplierAdapter();
							int index = 0;
							for(int j = 0; j < supplierArray.getCount(); j++){
								if(supplier.equals(supplierArray.getItem(j))){
									index = j;
								}
							}
							itemSupplierSpinner.setSelection(index);
						}
						else if ( i == 6){
							setPicture(mCursor.getInt(mCursor.getColumnIndexOrThrow(x)));
						}
						
						i++;
					}
			    }
		    	else{
		    		clean();
		    	}
		    }
		    public void onNothingSelected(@SuppressWarnings("rawtypes") AdapterView parent) {
		      // Do nothing.
		    }
	}
	  
	private void clean(){
		this.itemItemSpinner.setSelection(0);
		this.itemProductSpinner.setSelection(0);
		this.itemSupplierSpinner.setSelection(0);
		itemName.setText("");
		itemStatus.setText(""); 
		itemListPrice.setText(""); 
		itemUnitCost.setText("");
		setPicture(0);
	}
    
	private void setSupplierSpinner() {
		itemSupplierSpinner.setAdapter(getSupplierAdapter());
	}
	
	  @SuppressWarnings("unchecked")
		public ArrayAdapter<CharSequence> getSupplierAdapter(){
				    @SuppressWarnings("rawtypes")
					ArrayAdapter itemAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, dh.selectAllSuppliers());	 
			        itemAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			        itemAdapter.insert(" ", 0);
			        return itemAdapter;
		}

	private void setItemSpinner() {
		itemItemSpinner.setAdapter(getItemAdapter());
	}
	
	  @SuppressWarnings("unchecked")
		public ArrayAdapter<CharSequence> getItemAdapter(){
				    @SuppressWarnings("rawtypes")
					ArrayAdapter itemAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, dh.selectAllItems());	 
			        itemAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			        itemAdapter.insert(" ", 0);
			        return itemAdapter;
		}

	private void setProductSpinner() {
		itemProductSpinner.setAdapter(getProductAdapter());
	}
	
	  @SuppressWarnings("unchecked")
		public ArrayAdapter<CharSequence> getProductAdapter(){
				    @SuppressWarnings("rawtypes")
					ArrayAdapter itemAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, dh.selectAllProducts());	 
			        itemAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			        itemAdapter.insert(" ", 0);
			        return itemAdapter;
		}

		public  void openCategory(View view){
			Intent launchCategory = new Intent(this, UpdateDatabaseCategoryActivity.class);
			startActivityForResult(launchCategory, 1);
		}
		
		public void openItem(View view){
			Intent launchItem = new Intent(this, UpdateDatabaseItemActivity.class);
			startActivityForResult(launchItem, 1);
		}
		
		public void openProduct(View view){
			Intent launchProduct = new Intent(this, UpdateDatabaseProductActivity.class);
			startActivityForResult(launchProduct, 1);
		}
		
		public void openQOH(View view){
			Intent launchQOH = new Intent(this, UpdateDatabaseQOHActivity.class);
			startActivityForResult(launchQOH, 1);
		}
		
		public void openSupplier(View view){
			Intent launchSupplier = new Intent(this, UpdateDatabaseSupplierActivity.class);
			startActivityForResult(launchSupplier, 1);
		}
    
    public void doAddItem(View view){
    	dh.insertItem(itemProductSpinner.getSelectedItem().toString(), itemSupplierSpinner.getSelectedItem().toString(),
    			itemName.getText().toString(), itemStatus.getText().toString(), 
    			Float.parseFloat(itemListPrice.getText().toString()), 
    			Float.parseFloat(itemUnitCost.getText().toString()),
    			this.imageInteger);
    }
    
    public void doDeleteItem(View view){
    	dh.deleteItem(itemItemSpinner.getSelectedItem().toString());
    }
    
    public void doUpdateItem(View view){
    	this.doDeleteItem(view);
    	this.doAddItem(view);
    }
    
	public void doSelectPicture(View view){
		Intent launchSelectPicture = new Intent(this, SelectPictureActivity.class);
		startActivityForResult(launchSelectPicture, 0);
	}
	
	protected void onActivityResult(int requestCode,int resultCode, Intent data) {
		if (resultCode == RESULT_OK) 
		{
			int pictureID = data.getExtras().getInt("pictureNumber");
			setPicture(pictureID);	
		}
		else{
			Toast
			  .makeText(this, "Picture not passed back!",Toast.LENGTH_LONG)
			  .show();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	private void setPicture(int pictureID) {
		if(pictureID > -1 && pictureID < 14){
			itemImageView.setImageResource(Imgid[pictureID]);
			imageInteger = pictureID;
		}
		else{
			Toast
			  .makeText(this, "Picture reference is incorrect!",Toast.LENGTH_LONG)
			  .show();
		}
	}
    
}
